package GUI;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JButton;

import MessagePacks.FreezeUnfreezePack;
import MessagePacks.MessagePack;
import MessagePacks.UpdatePack;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Iterator;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class DefrostProcess extends IS_dep_manager {
	
	private static final long serialVersionUID = 1L;
	private JButton btnDefrost;
	private MainGUI mainGui;
	private JComboBox<String> processBox;
	private FreezeUnfreezePack unFrPack;
	private UpdatePack updateStat;
	private UpdatePack updateDate;
	
	public DefrostProcess(MainGUI mainGui){
		super(mainGui);
		this.mainGui= mainGui;
		getProccessSelect();
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 884, 40);
		getContentPane().add(panel);
		
		JLabel lblDefrostProcess = new JLabel("Defrost Process");
		lblDefrostProcess.setFont(new Font("Dialog", Font.BOLD, 16));
		panel.add(lblDefrostProcess);
		
		JLabel lblSelectProcess = new JLabel("Select Process");
		lblSelectProcess.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSelectProcess.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblSelectProcess.setBounds(260, 233, 113, 23);
		getContentPane().add(lblSelectProcess);
		
		setBtnDefrost();
		
		processBox = new JComboBox<String>();
		processBox.addItem("");
		Iterator<String> iter = unFrPack.nMap.iterator();
		
		while (iter.hasNext()) {
			String procId = iter.next();
				processBox.addItem(procId);

		}
		processBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				btnDefrost.setEnabled(true);
			}
		});
		processBox.setBounds(383, 236, 139, 20);
		getContentPane().add(processBox);
		
		
		
		
	}
	
	public JButton getBtnDefrost() {
		return btnDefrost;
	}
	public void setBtnDefrost() {
		btnDefrost = new JButton("Defrost");
		btnDefrost.setEnabled(false);
		btnDefrost.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(serverGUI.mainframe,"Process Active ");
				close();
				IS_dep_manager ManagerMenu = new IS_dep_manager(mainGui);
				ManagerMenu.setVisible(true);
				updStat();
				updateUnSuspDate();
			}
		});
		btnDefrost.setBounds(403, 280, 89, 23);
		getContentPane().add(btnDefrost);
	}
	
	public void getProccessSelect(){
		unFrPack = new FreezeUnfreezePack(MessagePack.unfreeze);
		mainGui.client.handleMessageFromClientUI(unFrPack);
		unFrPack= (FreezeUnfreezePack) mainGui.client.getMessage();
		
	}
	
	public void updateUnSuspDate(){
		updateDate = new UpdatePack(MessagePack.updateUnsusDate);
		updateDate.setProcId(Integer.parseInt((String) processBox.getSelectedItem()));
		mainGui.client.handleMessageFromClientUI(updateDate);
	}
	
	
	public void updStat(){
		updateStat = new UpdatePack(MessagePack.updateStatus);
		updateStat.setStatus("Active");
		updateStat.setProcId(Integer.parseInt((String) processBox.getSelectedItem()));
		mainGui.client.handleMessageFromClientUI(updateStat);
	}

	
}
